home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / macbinar / mymacbin.uni < prev    next >
Text File  |  1992-12-06  |  4KB  |  141 lines

  1. unit MyMacBinary;
  2.  
  3. interface
  4.  
  5.     uses
  6.         CRCs;
  7.  
  8.     const
  9.         macbin_folder_ftype = 'fold';
  10.         macbin_folder_creator_start = -1; { Should be OSType(-1), damn TP! }
  11.         macbin_folder_creator_end = -2;
  12.  
  13.     type
  14.         MBpacket = packed array[1..128] of byte;
  15.  
  16.     type
  17.         MBIIStartHeader = packed record
  18.                 name: str63;
  19.                 ftype: OSType;
  20.                 fcreator: OSType;
  21.                 flags_high: byte;
  22.                 zero1: byte;
  23.                 flocation: Point;
  24.                 windowID: integer; {? - ignore }
  25.                 protected: byte; { low order bit - ignore}
  26.                 zero2: byte;
  27.                 dlen: longInt;
  28.                 rlen: longInt;
  29.                 create_date: longInt;
  30.                 mod_date: longInt;
  31.                 clen: integer;
  32.                 flags_low: byte;
  33.             end;
  34.         MBIIHeader = packed record
  35.                 version: signedByte;
  36.                 MBIIStart: signedByte;
  37.                 space: packed array[2..115] of byte;
  38.                 total_unpack_len: longInt;{ignore}
  39.                 second_header_len: integer;{ignore}
  40.                 versionII: byte;
  41.                 minversionII: byte;
  42.                 crc: integer;
  43.                 processorID: integer; {ignore}
  44.             end;
  45.         packet_type = (PT_None, PT_File, PT_StartBlock, PT_EndBlock);
  46.  
  47.     function ValidateMBHeader (var header: MBIIHeader; handle2plus: boolean): packet_type;
  48.     procedure CatInfo2MBHeader (var pb: CInfoPBRec; var header: MBIIHeader; dtdbr: integer; var comment: str255);
  49.  
  50. implementation
  51.  
  52.     uses
  53.         MyMemory, MyDesktopDB;
  54.  
  55.     function ValidateMBHeader (var header: MBIIHeader; handle2plus: boolean): packet_type;
  56.         var
  57.             ocrc, i: integer;
  58.             typ: packet_type;
  59.             start: MBIIStartHeader;
  60.     begin
  61.         BlockMove(@header.MBIIStart, @start, SizeOf(start));
  62.         typ := PT_None;
  63.         with header do begin
  64.             if (version <= ord(handle2plus)) & (MBpacket(header)[75] = 0) then begin
  65.                 ocrc := 0;
  66.                 for i := 1 to 124 do
  67.                     CalcMBCRC(ocrc, MBpacket(header)[i]);
  68.                 if ocrc = MBIIHeader(header).crc then begin
  69.                     if (version = 1) & (start.ftype = macbin_folder_ftype) & ((start.fcreator = OSType(macbin_folder_creator_start)) or (start.fcreator = OSType(macbin_folder_creator_end))) then begin
  70.                         if start.fcreator = OSType(macbin_folder_creator_start) then
  71.                             typ := PT_StartBlock
  72.                         else
  73.                             typ := PT_EndBlock;
  74.                     end
  75.                     else begin
  76.                         typ := PT_File;
  77.                     end;
  78.                 end
  79.                 else begin { Assume its a valid MacBinary I file }
  80.                     if (version = 0) & (MBpacket(header)[2] >= 1) & (MBpacket(header)[2] <= 31) then begin
  81.                         MBpacket(header)[101] := 0; { Zero out the flags low_byte }
  82.                         total_unpack_len := 0;
  83.                         second_header_len := 0;
  84.                         versionII := 129;
  85.                         minversionII := 129;
  86.                         crc := 0;
  87.                         processorID := 0;
  88.                     end;
  89.                 end;
  90.             end;
  91.         end;
  92.         ValidateMBHeader := typ;
  93.     end;
  94.  
  95.     procedure CatInfo2MBHeader (var pb: CInfoPBRec; var header: MBIIHeader; dtdbr: integer; var comment: str255);
  96.         var
  97.             start: MBIIStartHeader;
  98.             fs: FSSpec;
  99.             folder: boolean;
  100.             ocrc, i: integer;
  101.     begin
  102.         folder := BAND(pb.ioFlAttrib, $10) <> 0;
  103.         MFillLong(@header, SizeOf(header), 0);
  104.         MFill(@start, SizeOf(start), 0);
  105.         header.version := ord(folder);
  106.         header.versionII := 129 + ord(folder);
  107.         header.minversionII := 129 + ord(folder);
  108.         start.name := pb.ioNamePtr^;
  109.         start.flags_high := BAND(BSR(pb.ioFlFndrInfo.fdFlags, 8), $FF);
  110.         start.flags_low := BAND(pb.ioFlFndrInfo.fdFlags, $FF);
  111.         start.flocation := pb.ioFlFndrInfo.fdLocation;
  112.         start.windowID := pb.ioFlFndrInfo.fdFldr;
  113.         start.create_date := pb.ioFlCrDat;
  114.         start.mod_date := pb.ioFlMdDat;
  115.  
  116.         if folder then begin
  117.             start.ftype := macbin_folder_ftype;
  118.             start.fcreator := OSType(macbin_folder_creator_start);
  119.             start.dlen := 0;
  120.             start.rlen := 0;
  121.         end
  122.         else begin
  123.             start.ftype := pb.ioFlFndrInfo.fdType;
  124.             start.fcreator := pb.ioFlFndrInfo.fdCreator;
  125.             start.dlen := pb.ioFlLgLen;
  126.             start.rlen := pb.ioFlRLgLen;
  127.         end;
  128.  
  129.         fs.vRefNum := pb.ioVRefNum;
  130.         fs.parID := pb.ioFlParID;
  131.         fs.name := pb.ioNamePtr^;
  132.         GetDTDBComment(dtdbr, fs, comment);
  133.         start.clen := length(comment);
  134.         BlockMove(@start, @header.MBIIStart, SizeOf(start));
  135.         ocrc := 0;
  136.         for i := 1 to 124 do
  137.             CalcMBCRC(ocrc, MBPacket(header)[i]);
  138.         header.crc := ocrc;
  139.     end;
  140.  
  141. end.